package com.huanqiu.mylib.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

import androidx.appcompat.app.AppCompatActivity;

import com.huanqiu.mylib.R;
import com.huanqiu.mylib.view.CommonHeaderView;

public class WebActivity extends AppCompatActivity {

    private static final String TAG = "WebActivity";

    private static Context context;

    private String title;
    private String url;

    private CommonHeaderView headerView;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        url = intent.getStringExtra("url");
        Log.d(TAG, "title = " + title);
        Log.d(TAG, "url = " + url);

        initView();
    }

    private void initView() {
        headerView = findViewById(R.id.header_view);
        webView = findViewById(R.id.webView);

        headerView.setTitle(title);
        headerView.setLeftListener(view -> {
            this.onBackPressed();
        });
        webView.loadUrl(url);
    }

    public static void init(Context context) {
        WebActivity.context = context;
    }

    public static void open(String title, String url) {
        Intent intent = new Intent(WebActivity.context, WebActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        WebActivity.context.startActivity(intent);
    }

}
